package com.qf.common.utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitUtils {


    /**
     * 获取rabbit 连接
     * @return
     */
    public static Connection getConnection(){

        // ConnectionFactory 创建连接的工厂 ，使用的就是工厂模式
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("121.41.169.157");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        // VirtualHost 表示消息的存储位置  类似于文件，默认是 /
        connectionFactory.setVirtualHost("/");


        try {
            // 创建连接
            Connection connection = connectionFactory.newConnection();
            return connection;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return null;
    }

}
